const mongoose = require('mongoose');
const Helps = require('../models/helps.model');

//新增
exports.create = function(req, res, next) {
    const helps = new Helps(req.body);
    helps.save().then((data) => {
        res.json(data);
    })
}

exports.update = function(req, res, next) {
    const id = req.params.id;
    Helps.findByIdAndUpdate(id, { $set: req.body }, { new: false }).then(data => {
        res.json(data);
    })
}


exports.remove = function(req, res, next) {
    const id = req.params.id;
    Helps.findByIdAndDelete(id, (err, data) => {
        res.json(data);
    })
}

exports.list = function(req, res, next) {
    var page = req.body.page ? req.body.page : 1;
    var rows = req.body.rows ? req.body.rows : 1;

    var queryCondition = {}; //查询条件
    if (req.body.articleTitle && req.body.articleTitle.trim().length > 0) {
        articleTitle = req.body.articleTitle;
        queryCondition = {
            "articleTitle": new RegExp(articleTitle, 'i')
        }
    }
    Helps.paginate(queryCondition, { page: +page, limit: +1000 }, function(err, result) {
        result.rows = result.docs;
        delete result.docs;
        res.json(result);
    })
}

exports.getdata = function(req, res, next) {
    var id = req.params.id;
    Helps.findById(id).then(data => {
        res.json(data);
    })
}
exports.removes = function(req, res, next) {
    var ids = req.body.ids.split(',');
    if (ids.length > 0) {
        Helps.remove({ _id: { $in: ids } }).then(data => {
            res.json({ 'message': 'delete ok', status: 200 })
        })
    }

}